﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;

namespace AbpTemplates
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("请输入项目名称，例如：AbpTemplates，TianQuan.AbpTemplates。");
                return;
            }

            string name = args[0];
            string complanyName = string.Empty;
            string projectName = name;
            if (name.Contains("."))
            {
                complanyName = name.Substring(0, name.IndexOf(".")).Replace(".", "");
                projectName = name.Substring(name.IndexOf(".")).Replace(".", "");
            }
            //设定APB源码路径
            string templates = @"src";

            if (Directory.Exists(name))
            {
                Directory.CreateDirectory(name);
            }

            Console.WriteLine("正在生成...");

            string targetDir = Environment.CurrentDirectory + @"\" + name;
            TemplateDirectoryCopy(templates, targetDir, complanyName, projectName);

            Console.WriteLine("生成模板完成。");

        }


        static void TemplateDirectoryCopy(string templateDir, string targetDir, string complanyName, string projectName)
        {
            DirectoryInfo templateDirectories = new DirectoryInfo(templateDir);
            DirectoryInfo targetDirectories = new DirectoryInfo(targetDir);

            string nameSpace = complanyName + "." + projectName;
            nameSpace = nameSpace.Trim('.');

            if (targetDirectories.FullName.StartsWith(templateDirectories.FullName, StringComparison.CurrentCultureIgnoreCase))
            {
                throw new Exception("父目录不能拷贝到子目录！");
            }

            if (!templateDirectories.Exists)
            {
                return;
            }



            string newTargetDir = targetDirectories.FullName.Replace("MyCompanyName.AbpZeroTemplate", nameSpace);
            newTargetDir = newTargetDir.Replace("AbpZeroTemplate", projectName);
            if (!System.IO.Directory.Exists(newTargetDir))
            {
                Directory.CreateDirectory(newTargetDir);
            }


            FileInfo[] templateFileInfos = templateDirectories.GetFiles();


            Parallel.ForEach(templateFileInfos, fileInfo =>
            {
                TemplateFileCopy(projectName, fileInfo, nameSpace, newTargetDir, complanyName);
            });

            Parallel.ForEach(templateDirectories.GetDirectories(), directoryInfo =>
            {
                TemplateDirectoryCopy(directoryInfo.FullName, newTargetDir + @"\" + directoryInfo.Name, complanyName,
                    projectName);
            });
        }

        private static void TemplateFileCopy(string projectName, FileInfo fileInfo, string nameSpace, string newTargetDir, string complanyName)
        {
            var source = File.ReadAllText(fileInfo.FullName, EncodingType.GetType(fileInfo.FullName));
            String extensionName = Path.GetExtension(fileInfo.Name).ToLower();

            switch (extensionName)
            {
                case ".cs":
                    source = source.Replace("MyCompanyName.AbpZeroTemplate", nameSpace);
                    source = source.Replace("AbpZeroTemplate", projectName);
                    File.WriteAllText(newTargetDir + @"\" + fileInfo.Name.Replace("AbpZeroTemplate", projectName), source,
                        Encoding.UTF8);
                    break;
                case ".csproj":
                    source = source.Replace("MyCompanyName.AbpZeroTemplate", nameSpace);
                    source = source.Replace("AbpZeroTemplate", projectName);
                    File.WriteAllText(newTargetDir + @"\" + fileInfo.Name.Replace("MyCompanyName.AbpZeroTemplate", nameSpace),
                        source, Encoding.UTF8);
                    break;
                case ".sln":
                    source = source.Replace("MyCompanyName.AbpZeroTemplate", nameSpace);
                    File.WriteAllText(newTargetDir + @"\" + fileInfo.Name.Replace("MyCompanyName.AbpZeroTemplate", nameSpace),
                        source, Encoding.UTF8);
                    break;
                case ".xml":
                    source = source.Replace("AbpZeroTemplate", projectName);
                    File.WriteAllText(newTargetDir + @"\" + fileInfo.Name.Replace("AbpZeroTemplate", projectName), source,
                        Encoding.UTF8);
                    break;
                case ".cshtml":
                    source = source.Replace("MyCompanyName.AbpZeroTemplate", nameSpace);
                    source = source.Replace("<title>AbpZeroTemplate</title>",
                        string.Format("<title>{0}</title>", projectName));
                    source = source.Replace("AbpZeroTemplate", projectName);
                    File.WriteAllText(newTargetDir + @"\" + fileInfo.Name, source, Encoding.UTF8);
                    break;
                case ".config":
                    source = source.Replace("MyCompanyName.AbpZeroTemplate", nameSpace);
                    source = source.Replace("AbpZeroTemplate", projectName);
                    File.WriteAllText(newTargetDir + @"\" + fileInfo.Name, source, Encoding.UTF8);
                    break;
                case ".asax":
                    source = source.Replace("MyCompanyName.AbpZeroTemplate", nameSpace);
                    File.WriteAllText(newTargetDir + @"\" + fileInfo.Name, source, Encoding.UTF8);
                    break;
                case ".js":
                    source = source.Replace("AbpZeroTemplate", projectName);
                    File.WriteAllText(newTargetDir + @"\" + fileInfo.Name, source, Encoding.UTF8);
                    break;
                case ".html":
                    source = source.Replace("AbpZeroTemplate", projectName);
                    File.WriteAllText(newTargetDir + @"\" + fileInfo.Name, source, Encoding.UTF8);
                    break;
                case ".txt":
                    source = source.Replace("AbpZeroTemplate", projectName);
                    File.WriteAllText(newTargetDir + @"\" + fileInfo.Name, source, Encoding.UTF8);
                    break;
                case ".suo":
                    break;
                default:
                    File.Copy(fileInfo.FullName, newTargetDir + @"\" + fileInfo.Name, true);
                    break;
            }
        }
    }
}
